package org.NooLab.glue.net;

import java.util.Observer;

import org.NooLab.glue.components.DataReceiverIntf;
import org.NooLab.glue.components.DataSenderIntf;
import org.NooLab.utilities.net.pages.WebRetriever;



/**
 *  Writing a multi-threaded Socket Server
	http://www.javareference.com/jrexamples/viewexample.jsp?id=47
 * 
 * 
 */
public class ComBox implements ComBoxIntf{

	
	ComBoxProperties cProperties ;
	ComBoxFactory cfactory ;
	
	DataReceiverIntf receiver;
	DataSenderIntf sender, messageBoardSender ;
	
	WebRetriever webaddresscheck ;
	
	
	public ComBox( ComBoxFactory factory ){
		cfactory = factory ;
		
		webaddresscheck = new WebRetriever( cfactory.out.getPrintlevel() );
	}
	
	public boolean checkHttpConnectivity( BoardAddress boardAddress, String pageRequest){
		boolean rB=false;
		int timeout ;
		String host, responseStr, hostnameStr; 
		// we send a "Get" request to the address on the port,
		// in case of an error message we know that the machine is available 
		// but the board over there is not active
		
		host = "http://"+boardAddress.address.hostname; // +":" + boardAddress.address.port ;
		// host = "http://www.google.ch" ;
		
		webaddresscheck.setUrlStr(host, boardAddress.address.port, pageRequest);
		webaddresscheck.getHtml();
		
		timeout = 8000;
		hostnameStr = boardAddress.address.hostname.toLowerCase();
		hostnameStr = hostnameStr.replace("http://", "");
		if ((hostnameStr.startsWith("192.")) || 
				(hostnameStr.startsWith("10.")) || 
				(hostnameStr.startsWith("178.")) ||
				(hostnameStr.startsWith("168."))||
				(hostnameStr.startsWith("127."))||
				(hostnameStr.startsWith("localhost")) ){
			timeout = 1200;
		}
		
		webaddresscheck.setTimeout(timeout ) ; // TODO : ~8000 if servers are really remote !!
		
		responseStr = webaddresscheck.getHtmlStr();
		
		if (responseStr.length()>0){
			
			if ((responseStr.contains("HTTP_RESPONSE") ) && (responseStr.contains("-999") )){
				boardAddress.isAvailable = false;
			}else{
				boardAddress.isAvailable = true; // means: the machine is connectable and some http-server was responding
			}
			
			if (responseStr.contains("HTTP_RESPONSE")){
				
				boardAddress.isActive = false;
				
				if (responseStr.contains("401")){  // unauthorized
					boardAddress.isActive = true; 
				} 
				if (responseStr.contains("404")){} // page does not exist
			}else{
				boardAddress.isActive = true;
			}
			// check the return
			
		}else{
			boardAddress.isAvailable = false;
		}
		return rB;
	}
	
	public int getActiveTransportId() {
		return cfactory.getDRSLayer() ;
	}
	
	public void setComBoxProperties( ComBoxProperties cbprops ) {
		cProperties = cbprops;
		
	}

	public ComBoxProperties getComBoxProperties( ) {
		return cProperties ;
		
	}
	
	

	public ComBoxFactory getFactory() {
		 
		return cfactory ;
	}
	 
	@Override
	public DataReceiverIntf getReceiver() {
		 
		return receiver;
	}

	@Override
	public DataSenderIntf getSender() {
		 
		return sender;
	}
 
	@Override
	public void setReceiver(DataReceiverIntf receiver) {
	 
		this.receiver = receiver;
	}
 
	@Override
	public void setSender(DataSenderIntf sender) {
		this.sender = sender;
	}

	// TODO: needs also host somehow...
	public DataSenderIntf createSender( int port , int serviceport, String description) {
		
		sender = cfactory.createSender(port , serviceport, description) ;
		
		return sender;
	}


	@Override
	public DataReceiverIntf createReceiver( int listenport, int ctrlport,
											Observer observer, String description) {
		 
		  
		try{
			// in case of http-rest, listenport is a virtual listenport, not a physical one !  
			receiver = cfactory.createReceiver( listenport , ctrlport, observer, description ) ;
			
		}catch(Exception e){ 
			cfactory.out.printErr(2,"Creating a receiver was not possible, "+e.getMessage());
			receiver=null;
		}
		
		return receiver;
	}

	public void removeReceiver( DataReceiverIntf rreceiver){
		cfactory.removeReceiver(rreceiver) ;
	}
	
	
}
